package com.ruicar.afs.cloud.afscase.loanapprove.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseContractInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseContractInfoService;
import com.ruicar.afs.cloud.afscase.loanapprove.service.CasePriorityChangeService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ApplyStatusEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.PriorityEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>Description：案件优先级变更服务类</p>
 *
 * @author rongji.zhang
 * @Date 2020/10/11
 * @description
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class CasePriorityChangeServiceImpl implements CasePriorityChangeService {

    private final int defineResult=5;

    private CaseContractInfoService caseContractInfoService;

    @Override
    public String priorityChange(CaseContractInfo caseContractInfo) {
        String result = PriorityEnum.ORDINARY.getCode();
        Map<String ,Integer> resultMap = new TreeMap();
        if(ObjectUtils.isNotEmpty(caseContractInfo)){
            String overdueResult = this.getIsOverduePriority(caseContractInfo);
            resultMap.put(overdueResult,Integer.valueOf(overdueResult)-defineResult);
            String lockResult = this.getIsLockPriority(caseContractInfo);
            resultMap.put(lockResult,Integer.valueOf(lockResult)-defineResult);
            String applyStatusResult = this.getApplyStatusPriority(caseContractInfo);
            resultMap.put(applyStatusResult,Integer.valueOf(applyStatusResult)-defineResult);
            resultMap.keySet().removeIf(key -> key.equals(PriorityEnum.ORDINARY.getCode()));
            if(CollectionUtils.isNotEmpty(resultMap)){
                List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String, Integer>>(resultMap.entrySet());
                //然后通过比较器来实现排序
                Collections.sort(list,new Comparator<Map.Entry<String,Integer>>() {
                    //升序排序
                    public int compare(Map.Entry<String, Integer> o1,
                                       Map.Entry<String, Integer> o2) {
                        return o1.getValue().compareTo(o2.getValue());
                    }
                });
                for (Map.Entry<String, Integer> entry : resultMap.entrySet()) {
                    result = entry.getKey();
                    if (result != null) {
                        break;
                    }
                }
                return  result;

            } else {
                return result;
            }
        } else {
            return result;
        }
    }

    //超期判断优先级修改
    @Override
    public String getIsOverduePriority(CaseContractInfo contractInfo){
        if(StringUtils.isNotBlank(contractInfo.getIsOverdue())){
            if(contractInfo.getIsOverdue().equals(WhetherEnum.YES.getCode())){
                return PriorityEnum.OVERDUE.getCode();
            } else {
                return PriorityEnum.ORDINARY.getCode();
            }
        } else {
            return PriorityEnum.ORDINARY.getCode();
        }
    }

    //锁定判断优先级修改
    @Override
    public String getIsLockPriority(CaseContractInfo contractInfo) {
        if(StringUtils.isNotBlank(contractInfo.getIsLock())){
            if(contractInfo.getIsLock().equals(WhetherEnum.YES.getCode())){
                return PriorityEnum.LOWEST.getCode();
            } else {
                return PriorityEnum.ORDINARY.getCode();
            }
        } else {
            return PriorityEnum.ORDINARY.getCode();
        }
    }

    //补件回复优先级修改
    @Override
    public String getApplyStatusPriority(CaseContractInfo contractInfo) {
        if(StringUtils.isNotBlank(contractInfo.getApplyStatus())){
            if(contractInfo.getApplyStatus().equals(ApplyStatusEnum.LOAN_REPAIR.getState())){
                return PriorityEnum.HIGHEST.getCode();
            } else {
                return PriorityEnum.ORDINARY.getCode();
            }
        } else {
            return PriorityEnum.ORDINARY.getCode();
        }
    }
}
